package com.gitee.wsl.doc.prosemirror.ui.ext

import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.LinkInteractionListener
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.withStyle
import com.gitee.wsl.doc.prosemirror.model.node.Node
import com.gitee.wsl.doc.prosemirror.ui.InlineNodeStringBuilders
import com.gitee.wsl.doc.prosemirror.ui.MarkdownInlineTextContent
import com.gitee.wsl.doc.prosemirror.ui.TypographyStyle
import com.gitee.wsl.doc.prosemirror.ui.buildMarkdownInlineNodeString


fun AnnotatedString.Builder.buildMarkdownAnnotatedString(
    parent: Node,
    indentLevel: Int = 1,
    inlineContentMap: MutableMap<String, MarkdownInlineTextContent>,
    typographyStyle: TypographyStyle,
    inlineNodeStringBuilders: InlineNodeStringBuilders,
    linkInteractionListener: LinkInteractionListener? = null,
    isShowNotSupported: Boolean,
) {
    parent.forEach { node, offset, index ->
        val customBuilder = inlineNodeStringBuilders[node::class]
        customBuilder?.buildMarkdownInlineNodeString(
            node,
            inlineContentMap,
            typographyStyle,
            indentLevel,
            linkInteractionListener,
            inlineNodeStringBuilders,
            isShowNotSupported,
            this,
            ) ?: run {
            if (isShowNotSupported) {
                append("[Unsupported: ${node::class.simpleName}]")
            } else {
                append(node.text)
            }
        }
    }
    //var node = parent.firstChild
//    while (node != null) {
//        val customBuilder =
//            inlineNodeStringBuilders[node::class]
//        customBuilder?.buildMarkdownInlineNodeString(
//            node,
//            inlineContentMap,
//            typographyStyle,
//            indentLevel,
//            linkInteractionListener,
//            inlineNodeStringBuilders,
//            isShowNotSupported,
//            this,
//
//            ) ?: run {
//            if (isShowNotSupported) {
//                append("[Unsupported: ${node::class.simpleName}]")
//            } else {
//                append(node.text)
//            }
//        }
//        node = node.next
//    }
}


internal fun AnnotatedString.Builder.buildStyleString(
    node: Node,
    indentLevel: Int,
    inlineContentMap: MutableMap<String, MarkdownInlineTextContent>,
    typographyStyle: TypographyStyle,
    inlineNodeStringBuilders: InlineNodeStringBuilders,
    linkInteractionListener: LinkInteractionListener?,
    isShowNotSupported: Boolean,
    style: SpanStyle? = null,
) {
    val buildFun = {
        buildMarkdownAnnotatedString(
            node,
            indentLevel,
            inlineContentMap,
            typographyStyle,
            inlineNodeStringBuilders,
            linkInteractionListener,
            isShowNotSupported,
        )
    }
    if (style == null) {
        buildFun()
    } else {
        withStyle(style) {
            buildFun()
        }
    }

}